package edu.gatech.TNE.wonderLab7000.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import oracle.jdbc.pool.OracleDataSource;



public class WonderLogger {
	
	private Connection conn;
	private ResultSet result;
	private String query;
	private PreparedStatement statement;

	
	public Connection connect() throws SQLException {
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:ops$mdye3/wonderLab@acme.gatech.edu:1525:public");
        conn = ods.getConnection();
        return conn;
    }
	String name;
	public WonderLogger(String name)
	{
		this.name = name;
	}
	
	public static WonderLogger getLogger(String name)
	{
		return new WonderLogger(name);
	}

    // printing methods:

    public void info(String message) throws SQLException
    {
    	write("Info,"+ message + " " + now());
    }
    public void warn(String message) throws SQLException
    {
    	write("Warning,"+message + " " + now());
    }
    public void error(String message) throws SQLException
    {
    	write("ERROR," + message + " " + now());
    	
    }

   
	public String read(File file)
	{
		 StringBuilder contents = new StringBuilder();
		    
		    try {
		      //use buffering, reading one line at a time
		      //FileReader always assumes default encoding is OK!
		      BufferedReader input =  new BufferedReader(new FileReader(file));
		      try {
		        String line = null; //not declared within while loop
		        while (( line = input.readLine()) != null){
		          contents.append(line);
		          contents.append(System.getProperty("line.separator"));
		          //System.out.println(contents);
		        }
		      }
		      finally {
		        input.close();
		      }
		    }
		    catch (IOException ex){
		      ex.printStackTrace();
		    }
		    
		    return contents.toString();
		  }

	
	public void write(String message) throws SQLException
	{
		conn = connect();
		query = "INSERT INTO ERRORS VALUES('"+message+"')";
	   	statement = conn.prepareStatement(query);
	    try {
	    	result = statement.executeQuery();
		} catch (SQLException e) {
	        conn.close();
	        statement.close();
	        result.close();
	        System.out.println(e.toString());
		} 
	}
	  

	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

	  public static String now() {
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	    return sdf.format(cal.getTime());

	  }
	public static void main(String[] args) throws SQLException
	{
		
		WonderLogger log = new WonderLogger("My Logger");
		log.warn("This is just a test for warn");
		log.error("This is an error");
		log.info("This is info");
	}

	
	

}
